//
//  AddConnact2ViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/30.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class AddConnact2ViewController: BaseUIViewController {

    let textField1 = FakeTextfield()
    let textField2 = FakeTextfield()
    
    var model = AddConcatModel()
    var editModel: ConnactModel?
    
    init(editModel: ConnactModel? = nil, model: AddConcatModel) {
        super.init(nibName: nil, bundle: nil)
        self.editModel = editModel
        self.model = model
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = #colorLiteral(red: 0.9724436402, green: 0.972609818, blue: 0.9724331498, alpha: 1)
        self.navigationItem.title = "售后服务".localized
        
        textField1.config
            .addTo(view)
            .extra {
                $0.optional = true
                $0.placeHolder = "合同编号".localized
        }.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(5)
            make.height.equalTo(50)
        }
        
        textField2.config
            .addTo(view)
            .extra {
                $0.optional = true
                $0.placeHolder = "产品类型".localized
        }.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.height.equalTo(50)
            make.top.equalTo(textField1.snp.bottom).offset(1)
        }
        
        if let model = self.editModel {
            textField1.text = model.contract
            textField2.text = model.supplier
        }
        
        let button = UIButton().config
            .backgroundImage(UIImage.imageWithColor(APPConfig.baseColor))
            .title("下一步".localized)
            .cornerRadius(5)
            .clipsToBounds(true)
            .titleColor(.white)
            .addTarget(self, action: #selector(nextStep))
            .addTo(view)
            .makeConstraints { (make) in
                make.centerX.equalToSuperview()
                make.left.equalTo(40)
                make.height.equalTo(50)
                make.top.equalTo(textField2.snp.bottom).offset(65)
        }.view
        
        let text = NSMutableAttributedString.init(string: "跳过后可前往 反馈 入口补充售后服务信息， 以便我们为您提供更好的帮助。".localized)
        text.setTextColor(APPConfig.baseColor, text: "反馈".localized)
        
        let bottomLabel = UILabel()
        bottomLabel.textAlignment = .center
        bottomLabel.textColor = #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)
        bottomLabel.numberOfLines = 0
        bottomLabel.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        bottomLabel.attributedText = text
        self.view.addSubview(bottomLabel)
        bottomLabel.snp.makeConstraints { (make) in
            make.top.equalTo(button.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
            make.left.equalTo(75)
        }
        
        let hasFilled =
            Observable.combineLatest(textField1.rx.text.orEmpty.asObservable(),
                                     textField2.rx.text.orEmpty.asObservable())
            .map { (a, b) -> Bool in
                return a.count > 0 || b.count > 0
        }.share(replay: 1)
        
        hasFilled.bind(to: bottomLabel.rx.isHidden)
            .disposed(by: rx.disposeBag)
        
        hasFilled.map{ $0 ? "下一步".localized : "跳过".localized }
            .bind(to: button.rx.title(for: .normal))
            .disposed(by: rx.disposeBag)
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .baseColor
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.view.endEditing(true)
    }
    
    @objc func nextStep() {
        self.model.contract = self.textField1.text
        self.model.supplier = self.textField2.text
        self.navigationController?
            .pushViewController(AddConnact3ViewController(editModel: editModel, model: self.model), animated: true)
    }

}
